﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using BCNPCObject.DB;
using DbLinq;
using DbLinq.Oracle;
using CMISLibrary.Entities;
using CMISLibrary;


namespace BCNPCObject.EXCEL
{
    public class cls_HDG_DDO_SOGCS_Controller
    {
         #region   Atributes

        //private DataSet CMIS_Header = new CMISOutputParameter();
        private HDG_DDO_SOGCS info;

        #endregion

        #region   Constructor

        public cls_HDG_DDO_SOGCS_Controller()
        {
            info = new HDG_DDO_SOGCS();
        }
        public cls_HDG_DDO_SOGCS_Controller(HDG_DDO_SOGCS Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public HDG_DDO_SOGCS pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion
        #region Methods
        public bool check_sogcs(string ma_dviqly, string so_gcs,int thang, int nam)
        {
            try
            {
                

                var x = (from a in CMIS2.DB.D_SOGCS.ToList()
                         where a.MA_DVIQLY == ma_dviqly && a.MA_SOGCS == so_gcs && a.TINH_TRANG != 0
                         select a).ToList();
                if (x == null || x.Take(1).Count() == 0)
                {
                    return false;
                }
                var y = (from a in CMIS2.DB.GCS_LICHGCS.ToList()
                         where a.MA_DVIQLY == ma_dviqly && a.MA_SOGCS == so_gcs && a.THANG == thang && a.NAM == nam
                         select a).ToList();
                if (y == null && y.Take(1).Count() == 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                //maloi = ex.ToString();
                CMIS2.ResetDB(); return false;
            }
        }
        public bool insert_HDG_DDO_SOGCS(ref Int64 id_qhe, ref string maloi)
        {
            long _ID = CMIS2.DB.ExecuteCommand("Select SEQ_HDG_DDO_SOGCS.NEXTVAL from dual");
            if (_ID == -1) return false;
            try
            {
                info.ID_QHE = Convert.ToInt64(_ID);
                HDG_DDO_SOGCS objHDG_DDO_SOGCS = new HDG_DDO_SOGCS()
                {
                    MA_DVIQLY = info.MA_DVIQLY,
                    ID_QHE = info.ID_QHE,
                    MA_SOGCS = info.MA_SOGCS,
                    MA_DDO = info.MA_DDO,
                    MA_KHANG = info.MA_KHANG,
                    NGAY_HLUC = info.NGAY_HLUC,
                    MA_KVUC = info.MA_KVUC,
                    STT = info.STT,
                    KY = info.KY,
                    THANG = info.THANG,
                    NAM = info.NAM,
                    MA_CNANG = info.MA_CNANG,
                    NGAY_TAO = info.NGAY_TAO,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGAY_SUA = info.NGAY_SUA,
                    NGUOI_SUA = info.NGUOI_SUA
                };
                CMIS2.DB.HDG_DDO_SOGCS.InsertOnSubmit(info);
                CMIS2.DB.SubmitChanges();
                id_qhe = _ID;
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB();
                return false;
            }
        }
        public bool delete_HDG_DDO_SOGCS(DataTable dt, ref string maloi)
        {
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        var query = from p in CMIS2.DB.HDG_DDO_SOGCS
                                    where (p.MA_DVIQLY == dt.Rows[i]["MA_DVIQLY"].ToString() && p.ID_QHE == Convert.ToInt64(dt.Rows[i]["ID_QHE"]) && p.NGAY_HLUC == Convert.ToDateTime(dt.Rows[i]["NGAY_HLUC"]))
                                    select p;
                        if (query != null && query.Count() > 0)
                        {
                            CMIS2.DB.HDG_DDO_SOGCS.DeleteAllOnSubmit(query.ToList());
                            CMIS2.DB.SubmitChanges();
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB(); return false;
            }
        }
        #endregion
    }
}
